/*
 * @Author: 白川 杏 baiyufeisteam@163.com
 * @Date: 2023-05-19 15:55:25
 * @LastEditors: 白川 杏 baiyufeisteam@163.com
 * @LastEditTime: 2023-06-08 19:24:12
 * @FilePath: \herb-garden\backend\koaserver\routes\collection.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
//collect 收藏 projectwork
const router = require('koa-router')()

router.prefix('/collect')

//根据userid获取全部的收藏内容
router.post('/all', async (ctx, next) => {
  let userid = ctx.request.body.userid;
  let result = await ctx.db.EXCUTE('SELECT * FROM collect,guide WHERE userid=? and collect.guideid = guide.guideid', [userid]);
  console.log(result);
  if (result[0]) {
    ctx.body = {
      code: 200,
      data: result,
    }
  } else {
    ctx.body = {
      code: 303,
      msg: '暂时还没有收藏喔'
    }
  }
})

//根据collect_id获取的收藏内容
router.get('/single', async (ctx, next) => {
  let collect_id = ctx.request.body.collect_id;
  let result = await ctx.db.EXCUTE('SELECT * FROM collect WHERE collect_id =' + collect_id);
  ctx.body = {
    code: 200,
    data: result
  }
})



//backend
//页面初始化时获取数据库内的数据条目
router.post('/getcollectbackend', async (ctx, next) => {
  let resultuserid = await ctx.db.EXCUTE("SELECT (SELECT username FROM user ni WHERE n.userid=ni.userid) AS userinfo FROM collect n")
  let result = await ctx.db.EXCUTE("SELECT n.*, (SELECT title FROM guide ni WHERE n.guideid=ni.guideid) AS guideinfo FROM collect n")

  for (let i = 0; i < result.length; i++) {
    Object.assign(result[i], resultuserid[i]);
  }

  ctx.body = {
    code: 200,
    data: result
  }
})

module.exports = router